Why would you use a MyObject[] internally, but expose a List<MyObject>?
Posted
by timmyd
on Stack Overflow
See other posts from Stack Overflow
or by timmyd
Published on 2010-04-08T12:21:09Z
Indexed on
2010/04/08
12:23 UTC
Read the original article
Hit count: 121
I have come across a class that has an immutable property:
MyObject[] allObjs
The property is initialized like this:
List<MyObject> objs = createAllMyObjects();
allObjs = objs.toArray(new MyObject[objs.size()]);
When it is exposed through the accessor, it's done as a List:
public List<MyObject> getAllMyObjects() {
return Collections.unmodifiableList(Arrays.asList(allObjs));
}
Why would a programmer do this? Is there a benefit that I don't know about?
Performance is not a concern, as the array of objs will only ever number in a few dozen elements.
It seems that we are going round and round in circles.
The class is a sort of factory, so it's got a private constructor and exposes only static methods (not sure if this could be a reason for the madness).
© Stack Overflow or respective owner